package com.ruoyi.business.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.business.domain.TerminationAgreements;
import com.ruoyi.business.domain.vo.TerminationDetailVo;
import com.ruoyi.business.service.ITerminationAgreementsService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.business.domain.Contracts;
import com.ruoyi.business.service.IContractsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;



/**
 * 合同信息Controller
 *
 * @author ruoyi
 * @date 2025-02-13
 */

@RestController
@RequestMapping("/contracts")
public class ContractsController extends BaseController
{
    @Autowired
    private IContractsService contractsService;



    /**
     * 20250213 云计算-宏烨找房-05-王江波-我的合同任务工单V1.1-查询合同信息列表
     */
    @RequiresPermissions("business:contracts:list")
    @GetMapping("/list")
    public TableDataInfo list(Contracts contracts)
    {
        Long userId = SecurityUtils.getUserId();
        startPage();
        List<Contracts> list = contractsService.selectContractsList(contracts,userId);
        return getDataTable(list);
    }

    /**
     * 20250213 云计算-宏烨找房-05-王江波-我的合同任务工单V1.1-导出合同信息列表
     */
    @RequiresPermissions("business:contracts:export")
    @Log(title = "合同信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Contracts contracts)
    {
        List<Contracts> list = contractsService.selectContractsList(contracts,null);
        ExcelUtil<Contracts> util = new ExcelUtil<Contracts>(Contracts.class);
        util.exportExcel(response, list, "合同信息数据");
    }

    /**
     * 20250213 云计算-宏烨找房-05-王江波-我的合同任务工单V1.1-获取合同信息详细信息
     */
    @RequiresPermissions("business:contracts:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(contractsService.selectContractsById(id));
    }

    /**
     * 20250213 云计算-宏烨找房-05-王江波-我的合同任务工单V1.1-新增合同信息
     */
    @RequiresPermissions("business:contracts:add")
    @Log(title = "合同信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Contracts contracts)
    {
        Long userId = SecurityUtils.getUserId();
        return toAjax(contractsService.insertContracts(contracts,userId));
    }

    /**
     * 20250213 云计算-宏烨找房-05-王江波-我的合同任务工单V1.1-修改合同信息
     */
    @RequiresPermissions("business:contracts:edit")
    @Log(title = "合同信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Contracts contracts)
    {
        return toAjax(contractsService.updateContracts(contracts));
    }

    /**
     * 20250213 云计算-宏烨找房-05-王江波-我的合同任务工单V1.1-删除合同信息
     */
    @RequiresPermissions("business:contracts:remove")
    @Log(title = "合同信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(contractsService.deleteContractsByIds(ids));
    }

    /**
     * 手动触发更新合同状态
     *
     * @return 操作结果
     */
    @GetMapping("/update")
    public AjaxResult updateContractStatus() {
        try {
            contractsService.updateContractStatus();
            return AjaxResult.success("合同状态更新成功");
        } catch (Exception e) {
            return AjaxResult.error("合同状态更新失败：" + e.getMessage());
        }
    }

    /**
     * 获取需要检查状态的合同列表
     *
     * @return 合同列表
     */
    @GetMapping("/checkList")
    public AjaxResult getContractsForStatusCheck() {
        try {
            return AjaxResult.success(contractsService.selectContractsForStatusCheck());
        } catch (Exception e) {
            return AjaxResult.error("获取合同列表失败：" + e.getMessage());
        }
    }

    //查询所有的租客
    @RequiresPermissions("business:contracts:list")
    @GetMapping("/getUser")
    public AjaxResult getUser() {
        List<SysUser> userList=contractsService.selectUser();
        return AjaxResult.success(userList);
    }

    /*
      20250213 云计算-宏烨找房-05-王江波-我的合同任务工单V1.1-合同详情
      租赁条款：包括租金金额、支付周期、押金数额、租金递增条款等。
      双方信息：列出房东和租客的姓名、联系方式等基本信息。
      附加条款：如果有任何特殊约定或附加条款（如宠物政策、装修规定等），也应在详情中明确列出。
      电子签名：显示双方的电子签名及签署时间，证明合同的有效性和合法性
     */
    @RequiresPermissions("business:contracts:list")
    @GetMapping("/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Long id) {
        TerminationDetailVo contracts = contractsService.selectDetailById(id);
        return AjaxResult.success(contracts);
    }
}

